package ui.fragment.homepagefragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.angmarch.views.NiceSpinner;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Dailog.CommomDialog;
import adapter.PrePareAdapter;
import base.BaseBean;
import base.fragment.BaseNetFragment;
import bean.GetSubjectBean;
import bean.PrePareBeans;
import student.andlei.com.student.R;
import ui.activity.PrePareTaskActivity;
import utils.DateUtils;
import utils.GsonUtils;

/**
 * 预习
 */
public class PrePareFragment extends BaseNetFragment {
    private NiceSpinner spinner_alltime,
            spinner_allsubject,
            spinner_allstate;
    private SwipeRefreshLayout swipeRefreshLayout;
    private List<String> alltimeList = new ArrayList<>();
    private List<String> allsubjectList = new ArrayList<>();
    private List<GetSubjectBean> beanList;
    private List<String> allstateList = new ArrayList<>();
    private RecyclerView recyclerView;
    private List<PrePareBeans.mlist> lists = new ArrayList();
    private PrePareAdapter adapter;
    private int date = 0,
            subject_id = 0,
            is_prepare = 0;
    private int page = 1;
    private int page_number = 10;

    public static Fragment getInstance() {
        return new PrePareFragment();
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        hideLoadDialog();
        swipeRefreshLayout.setRefreshing(false);

        if (baseBean.code == 200) {
            if (action.equals("schedule/getSubject")) {
                beanList = GsonUtils.getInstance(getActivity()).parseNoHeaderJArray(baseBean, GetSubjectBean.class);
                GetSubjectBean bean0 = new GetSubjectBean();
                bean0.subject_name = "全部科目";
                bean0.subject_id = 0;
                beanList.add(0, bean0);
                for (GetSubjectBean bean : beanList) {
                    allsubjectList.add(bean.subject_name);
                }
                spinner_allsubject.attachDataSource(allsubjectList);
            } else {
                PrePareBeans bean = (PrePareBeans) GsonUtils.getInstance(getActivity()).parseComplexJArrayByCommon(baseBean, PrePareBeans.class);
                if (lists == null) {
                    lists = new ArrayList<>();
                }

//                if (page > bean.page.total_page) {
//                    LogUtil.app("已是最后一页");
//                } else {
                    if(page == 1){
                        lists = bean.list;
                        adapter.setNewData(bean.list);
                        if(bean.page == null){return;}
                        if(bean.page.total_page == 1){
                            adapter.loadMoreEnd();
                        }else {
                            page++;
                        }
                    }else {
                        if(page > bean.page.total_page){
                            adapter.loadMoreEnd();
                        }else {
                            lists.addAll(bean.list);
                            page++;
                            adapter.setNewData(lists);
                            adapter.loadMoreComplete();
                        }
                    }
                //}
            }
        } else {
            adapter.loadMoreEnd();
            toast(baseBean.msg);
        }

    }

    @Override
    public void error(String action, Throwable e) {
        hideLoadDialog();
        swipeRefreshLayout.setRefreshing(false);
        adapter.loadMoreFail();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_prepare;
    }

    @Override
    protected void findViews() {
        swipeRefreshLayout = getView(R.id.swipeRefreshLayout);
        spinner_allstate = getView(R.id.spn_allstate);
        spinner_alltime = getView(R.id.spn_alltime);
        spinner_allsubject = getView(R.id.spn_allsubject);
        recyclerView = getView(R.id.rcly_);
    }

    @Override
    protected void formatViews() {
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new PrePareAdapter(this.getActivity(), R.layout.item_prepare, lists);
        recyclerView.setAdapter(adapter);
        adapter.bindToRecyclerView(recyclerView);
        adapter.setEmptyView(R.layout.empty_view);
        //-----
        spinner_allstate.attachDataSource(allstateList);

        spinner_alltime.attachDataSource(alltimeList);
        //spinner点击
        spinner_alltime.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                date = i;
                page = 1;
                loaddata(date, subject_id, is_prepare);
                Log.i("PrePareFragment", "date----->" + date);
            }
        });
        spinner_allsubject.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (GetSubjectBean bean : beanList) {
                    TextView textView = (TextView) view;
                    if (textView.getText().toString().trim().equals("" + bean.subject_name)) {
                        subject_id = bean.subject_id;
                    }
                }
                page = 1;
                loaddata(date, subject_id, is_prepare);
                Log.i("PrePareFragment", "subject_id----->" + subject_id);
            }
        });
        spinner_allstate.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                is_prepare = i;
                page = 1;
                loaddata(date, subject_id, is_prepare);
                Log.i("PrePareFragment", "is_prepare----->" + is_prepare);
            }
        });
        //recyclerView点击
//        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Bundle bundle = new Bundle();
//                bundle.putInt("room_id",lists.get(position).room_id);
//                jumpTo(PrePareTaskActivity.class, bundle);
//            }
//        });
        //下拉刷新
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //lists.clear();
                page = 1;
                loaddata(date, subject_id, is_prepare);
            }
        });
        //上拉加载
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loaddata(date, subject_id, is_prepare);
                    }
                }, 500);

            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.bt_look:
                        if (lists.get(position).bag_id == 0) {
                            new CommomDialog(getActivity(), R.style.dialog, "老师暂无备课包，无法进入预习任务", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    if (confirm) {
                                        dialog.dismiss();
                                    }
                                }
                            }).setTitle("提示").hideNegativeButton().settPositiveButtonColor(R.color.homepage_titlefont).show();
                        } else {
                            //格式化时间
                            try {
                                SimpleDateFormat CurrentTime = new SimpleDateFormat("HH:mm");
                                Date beginTime = CurrentTime.parse(lists.get(position).start_time);
                                Date now = CurrentTime.parse(DateUtils.getCurrentDatefour());
                                Date endTime = CurrentTime.parse(lists.get(position).end_time);
                                if (beginTime.getTime() < now.getTime() && now.getTime() < endTime.getTime()) {
                                    new CommomDialog(getActivity(), R.style.dialog, "正在上课中，请进入课堂", new CommomDialog.OnCloseListener() {
                                        @Override
                                        public void onClick(Dialog dialog, boolean confirm) {
                                            if (confirm) {
                                                dialog.dismiss();
                                            }
                                        }
                                    }).setTitle("提示").hideNegativeButton().settPositiveButtonColor(R.color.homepage_titlefont).show();

                                } else {
                                    Bundle bundle_ = new Bundle();
                                    bundle_.putInt("room_id", lists.get(position).room_id);
                                    jumpTo(PrePareTaskActivity.class, bundle_);
                                }
                            } catch (Exception e) {

                            }
//                            Bundle bundle = new Bundle();
//                            bundle.putInt("room_id",lists.get(position).room_id);
//                            jumpTo(PrePareTaskActivity.class, bundle);
                        }
                        break;
                }
            }
        });
    }

    @Override
    protected void formatData() {
//        for(int i=0;i<10;i++){
//            PrePareBeans beans = new PrePareBeans();
//            beans.setSubject("信息技术");
//            beans.setDate("2018-15-0"+i);
//            beans.setTeacher("刘静老师");
//            beans.setState("已预习");
//            beans.setWeek("星期一");
//            beans.setTime("19:00---20:00");
//            lists.add(beans);
//        }
        //-----时间选择
        alltimeList.add("全部时间");
        alltimeList.add("当天");
        alltimeList.add("本周");
        alltimeList.add("本月");
        //-----科目选择

        //------状态选择
        allstateList.add("全部状态");
        allstateList.add("已预习");
        allstateList.add("待预习");
        loaddata(0, 0, 0);
        Map<String, String> map = new HashMap<>();
        post("schedule/getSubject", map, GetSubjectBean.class, false);
    }

    private void loaddata(int date, int subject_id, int is_prepare) {
        //时间 0:全部[默认] 1:当天 2:当周 3:当月
        // 科目id 0:全部[默认]
        // 0:全部[默认] 1:表示已经预习复习过，2:表示未预习复习过
        Map<String, String> map = new HashMap<>();
        map.put("subject_id", "" + subject_id);
        map.put("date", "" + date);
        map.put("is_prepare", is_prepare + "");
        map.put("page", page + "");
        map.put("page_number", page_number + "");
        post("schedule/preparation", map, PrePareBeans.class, true);
    }

    @Override
    protected void getBundle() {

    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            loaddata(date, subject_id, is_prepare);
        }
    }
}
